<?php

namespace App\Jobs\Stats\User;

use App\Jobs\Stats\Stats as JobStats;
use App\Models\Admin\Stats\User\Day as stats;
use App\Models\Game\Settlement\Info;
use App\Models\Game\Log\UserLogin;
use App\Models\Game\User\User;

class Day extends JobStats
{

    /**
     * 执行统计任务。
     *
     * @return void
     */
    public function stats()
    {
        stats::updateOrCreate(
            $this->task->only(['timed', 'plat_code']),
            [
                // 游戏人数
                'u_game' => $this->stats_u_game(),
                // 登录人数
                'u_sign' => $this->stats_u_sign(),
                // 新增游戏人数
                'nu_game' => $this->stats_nu_game(),
                // 注册人数
                'nu_sign' => $this->stats_nu_sign(),
            ]
        );
    }

    /**
     * 游戏人数
     */
    protected function stats_u_game()
    {
        return Info::dbTable()
            ->where('status', Info::STATUS_NORMAL)
            ->where('player_type', Info::PLAYER_TYPE)
            ->where('end_time', '<', $this->task->timed + 86400)
            ->where('end_time', '>=', $this->task->timed)
            ->where('plat_code', $this->task->plat_code)
            ->distinct('uid')
            ->count();
    }

    /**
     * 登录人数
     */
    protected function stats_u_sign()
    {
        return UserLogin::dbTable()
            ->where('login_time', '<', $this->task->timed + 86400)
            ->where('login_time', '>=', $this->task->timed)
            ->where('plat_code', $this->task->plat_code)
            ->distinct('uid')
            ->count();
    }

    /**
     * 新增游戏人数
     */
    protected function stats_nu_game()
    {
        return User::dbTable()
            ->where('first_game_time', '<', $this->task->timed + 86400)
            ->where('first_game_time', '>=', $this->task->timed)
            ->where('plat_code', $this->task->plat_code)
            ->count();
    }

    /**
     * 注册人数
     */
    protected function stats_nu_sign()
    {
        return User::dbTable()
            ->where('registration_time', '<', $this->task->timed + 86400)
            ->where('registration_time', '>=', $this->task->timed)
            ->where('plat_code', $this->task->plat_code)
            ->count();
    }
}
